package com.szqhyxj.youxiangju.biz.onlineChannel.onlineReceipt;

import com.szqhyxj.youxiangju.biz.receipt.receiptdetail.ReceiptDetailModel;
import com.szqhyxj.youxiangju.sys.AbBasePresenter;
import com.szqhyxj.youxiangju.sys.BaseResult;

import java.math.BigDecimal;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by leung on 2018/12/25.
 */

public class OnlineReceiptPresenter extends AbBasePresenter implements OnlineReceiptContract.presenter {

    private OnlineReceiptContract.View view;

    private OnlineReceiptModel.OnlineReceiptBean onlineReceiptBean;

    public OnlineReceiptPresenter(OnlineReceiptContract.View view) {
        this.view = view;
    }

    @Override
    public void onDestroy() {

    }

    @Override
    public void init(String btCode, int channelId, String tradeType) {
        OnlineReceiptModel.getChannelContent(btCode, channelId, tradeType)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Consumer<BaseResult<OnlineReceiptModel.OnlineReceiptBean>>() {
                    @Override
                    public void accept(BaseResult<OnlineReceiptModel.OnlineReceiptBean> onlineReceiptBeanBaseResult) throws Exception {
                        if (onlineReceiptBeanBaseResult.businessLogicSuccess()) {

                          //  view.showContent(onlineReceiptBeanBaseResult.getData());
                         //   onlineReceiptBean = onlineReceiptBeanBaseResult.getData();
                        } else {
                            view.showToast(onlineReceiptBeanBaseResult.getData() + "");
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        view.showToast(throwable.getMessage() + "");
                    }
                });
    }

    @Override
    public void getdeposit() {

    }

    @Override
    public void setAmount(String amount, String money, int minamount, int maxamount, String settleFee) {
        if (amount == null || amount.equals("")) {
            view.showHandingFee("0.00" + "元");
            return;
        }
        double receiptfee = new Double(settleFee);
        if (Double.parseDouble(amount) > Double.parseDouble(money)) {
            view.showEnClickable();
            double fee = Double.parseDouble(amount) * receiptfee;
            BigDecimal b = new BigDecimal(fee);
            double fee1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
            view.showHandingFee(String.valueOf(fee1) + "元");
            return;
        } else {


            if (Double.parseDouble(amount) < minamount || Double.parseDouble(amount) > maxamount || amount.equals("")) {
                view.showEnClickable();
            } else {
                double fee = Double.parseDouble(amount) * receiptfee;
                BigDecimal b = new BigDecimal(fee);
                double fee1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                view.showHandingFee(String.valueOf(fee1) + "元");
                view.showClickable();
            }
        }

    }
}
